// main.js
// Modules to control application life and create native browser window
const {
  app,
  BrowserWindow,
  ipcMain,
  Menu,
  Tray,
  globalShortcut,
} = require("electron");
const path = require("path");
const Store = require("electron-store");
const store = new Store();
const { createNewWindow, closeWindow } = require("./createNewWindow.js");
const icons = `${path.join(__dirname, "../src/assets/images/logo/logo2.ico")}`;
const env = process.env.NODE_ENV;
let tray = null;
var count = 0,
  timer = null;
var count1 = 0,
  timer1 = null;

let mainWindows = null;

const createWindow = () => {
  // Create the browser window.
  const mainWindow = new BrowserWindow({
    width: 1360,
    height: 910,
    minWidth: 1300,
    minHeight: 860,
    frame: false,
    modal: true,
    icon: icons,
    webPreferences: {
      nodeIntegration: true,
      contextIsolation: false,
      enableRemoteModule: true,
    },
  });

  require("./ipcMain.js");

  mainWindows = mainWindow;

  // 加载 index.html
  // mainWindow.loadURL("http://127.0.0.1:5173/");
  // console.log(path.join(__dirname, "../dist/index.html"));
  // mainWindow.loadURL(path.join(__dirname, "../dist/index.html"));
  const loadUrl =
    env == "development"
      ? "http://127.0.0.1:5173/"
      : path.join(__dirname, "../dist/index.html");
  mainWindow.loadURL(loadUrl);

  createNewWindow({ type: 0, mainWin: mainWindow });

  store.set("home", mainWindow.webContents.id);

  // 打开开发工具
  if (env == "development") {
    mainWindow.webContents.openDevTools();
  }

  if (tray == null) {
    //托盘
    tray = new Tray(icons);
    mainWindow.on("show", () => {
      clearInterval(timer);
      clearInterval(timer1);
      tray.setImage(icons);
    });
    const contextMenu = Menu.buildFromTemplate([
      {
        label: "退出",
        click: () => {
          app.quit();
        },
      },
    ]);
    tray.setToolTip("yqg-music");
    tray.setContextMenu(contextMenu);
    tray.on("click", () => {
      let getUrl = mainWindow.getURL();
      if (getUrl.indexOf("login") != -1) {
        return;
      }
      if (timer) {
        tray.setImage(icons);
        clearInterval(timer);
      }
      if (timer1) {
        tray.setImage(icons);
        clearInterval(timer1);
      }
      if (mainWindow.isVisible()) {
        if (mainWindow.isMinimized()) {
          mainWindow.show();
          clearInterval(timer);
          clearInterval(timer1);
        }
        if (!mainWindow.isFocused()) {
          mainWindow.show();
          mainWindow.setSkipTaskbar(false);
          clearInterval(timer);
          clearInterval(timer1);
        }
      } else {
        mainWindow.show();
        mainWindow.setSkipTaskbar(false);
        clearInterval(timer);
        clearInterval(timer1);
      }
    });
    ipcMain.on("send-msg-notify", function (event) {
      let currentWin = mainWindow;
      clearInterval(timer1);
      clearInterval(timer);
      if (!currentWin.isFocused()) {
        currentWin.flashFrame(true);
        timer1 = setInterval(function () {
          count1++;
          if (count1 % 2 == 0) {
            tray.setImage(icons);
          } else {
            tray.setImage(icons);
          }
        }, 500);
      } else {
        clearInterval(timer);
        clearInterval(timer1);
        if (currentWin.isVisible()) {
          clearInterval(timer);
        } else {
          timer = setInterval(function () {
            count++;
            if (count % 2 == 0) {
              tray.setImage(icons);
            } else {
              tray.setImage(icons);
            }
          }, 500);
        }
        if (currentWin.isMinimized()) {
          timer1 = setInterval(function () {
            count1++;
            if (count1 % 2 == 0) {
              tray.setImage(icons);
            } else {
              tray.setImage(icons);
            }
          }, 500);
        } else if (!currentWin.isMinimized()) {
          clearInterval(timer1);
        }
      }
    });
  }

  //关闭菜单
  mainWindow.setMenu(null);
};

const gotTheLock = app.requestSingleInstanceLock();
if (!gotTheLock) {
  app.quit();
} else {
  // Menu.setApplicationMenu(null);
  if (process.platform === "darwin") {
    const template = [
      {
        label: "Application",
        submenu: [
          {
            label: "Quit",
            accelerator: "Command+Q",
            click: function () {
              app.quit();
            },
          },
        ],
      },
      {
        label: "编辑",
        submenu: [
          { label: "复制", accelerator: "CmdOrCtrl+C", selector: "copy:" },
          { label: "粘贴", accelerator: "CmdOrCtrl+V", selector: "paste:" },
        ],
      },
    ];
    Menu.setApplicationMenu(Menu.buildFromTemplate(template));
  } else {
    Menu.setApplicationMenu(null);
  }
  app.whenReady().then(() => {
    globalShortcut.register("CmdOrCtrl+F12", () => {
      BrowserWindow.getFocusedWindow().webContents.openDevTools();
    });
    globalShortcut.register("Esc", () => {
      if (BrowserWindow.getFocusedWindow() == mainWindows) {
        return;
      } else {
        closeWindow(BrowserWindow.getFocusedWindow());
      }
    });
    //     //没有窗口时打开新建一个窗口
    app.on("activate", () => {
      if (BrowserWindow.getAllWindows().length === 0) createWindow();
    });
    createWindow();
  });
}

app.on("window-all-closed", () => {
  if (process.platform !== "darwin") app.quit();
});

ipcMain.on("window-close", (data) => {
  app.quit();
});
